﻿using System;
using System.IO;
using System.Linq;
using System.Configuration;

namespace Inovout.Resources
{
    internal class ConfigFile
    {
        private readonly string extensionName = "config";
        private readonly string path = "Configs";

        private string name;
        private Type type;
        public ConfigFile(string name, Type type)
        {
            ArgumentAssert.IsNotDefault(name, "name");
            ArgumentAssert.IsNotNull(type, "type");
            this.name = name;
            this.type = type;
        }
         public string FileFullName
        {
            get
            {
                string fileFullName;
#if DEBUG
                fileFullName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, string.Format("{0}\\{1}.debug.{2}", path, name, extensionName));
                if (File.Exists(fileFullName))
                {
                    return fileFullName;
                }
#endif
                fileFullName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, string.Format("{0}\\{1}.{2}", path, name, extensionName));
                if (File.Exists(fileFullName))
                {
                    return fileFullName;
                }
                return null;
            }
        }


        public Stream Read()
        {
            Stream stream = ReadFromFile();
            if (stream == null)
            {
                stream = ReadFromApplicaionConfigFile();
            }
            if (stream == null)
            {
                stream = ReadFromResource();
            }
            return stream;
        }

        private Stream ReadFromFile()
        {

            if (File.Exists(FileFullName))
            {
                return new FileStream(FileFullName, FileMode.Open, FileAccess.Read, FileShare.None);
            }

            else
            {
                return null;
            }
        }

        private Stream ReadFromApplicaionConfigFile()
        {
            if (ConfigurationManager.GetSection(name) != null)
            {
                //AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
                return null;
            }
            else
            {
                return null;
            }

        }
        private Stream ReadFromResource()
        {
            string configResourceName;
#if DEBUG
            configResourceName = string.Format("{0}.{1}.{2}.debug.{3}", type.Namespace,path, name, extensionName);
            if (type.Assembly.GetManifestResourceNames().Contains(configResourceName))
            {
                return type.Assembly.GetManifestResourceStream(configResourceName);
            }

#endif
            configResourceName = string.Format("{0}.Resources.{1}.{2}", type.Namespace, name, extensionName);
            if (type.Assembly.GetManifestResourceNames().Contains(configResourceName))
            {
                return type.Assembly.GetManifestResourceStream(configResourceName);
            }
            else
            {
                return null;
            }
        }
        //public void Write(Stream stream)
        //{
        //    string configFolderName = Path.GetDirectoryName(FileFullName);
        //    if (!Directory.Exists(configFolderName))
        //    {
        //        Directory.CreateDirectory(configFolderName);
        //    }
        //    using (FileStream fileStream = new FileStream(FileFullName, FileMode.CreateNew, FileAccess.Write, FileShare.None))
        //    {
        //        fileStream.Write(stream);
        //    }

        //}
    }
}
